package com.ztzh.temteam.controller;

import com.ztzh.temteam.base.SuccessModelResult;
import com.ztzh.temteam.example.Main;
import com.ztzh.temteam.utils.ChatDocUtil;
import com.ztzh.temteam.utils.PostFormUtil;
import com.ztzh.temteam.websocket.CustomWebSocketServer;
import org.java_websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/tenTeam")
public class TenTeamController {
   public   static LinkedList<String> fileId = new LinkedList<>();

    public static LinkedList<String> getFileId() {
        return fileId;
    }

    public static void setFileId(LinkedList<String> fileId) {
        TenTeamController.fileId = fileId;
    }

    @GetMapping("/start/{path}")
    public SuccessModelResult start(@PathVariable("path") Integer path) {
        List<String> all = new ArrayList<>();
        all.add("soft.docx");
        all.add("apple.pdf");
        fileId.addFirst(Main.start(all.get(path)));
        return SuccessModelResult.success(fileId);
    }

    @GetMapping("/startaLL")
    public SuccessModelResult startaLL() {
        Map<String, String> formData = new HashMap<>();
        formData.put("fileId", fileId.getFirst());

        String result = PostFormUtil.sendPostForm("https://chatdoc.xfyun.cn/openapi/v1/file/summary/start", formData);
        return SuccessModelResult.success(result);
    }



    @GetMapping("/getAll")
    public SuccessModelResult get() {
        Map<String, String> formData = new HashMap<>();
        formData.put("fileId", fileId.getFirst());

        String result = PostFormUtil.sendPostForm("https://chatdoc.xfyun.cn/openapi/v1/file/summary/query", formData);
        return SuccessModelResult.success(result);
    }

    @GetMapping("/getContent/{content}")
    public SuccessModelResult getContent(@PathVariable("content") String content) {
        Map<String, String> formData = new HashMap<>();
        formData.put("fileId", fileId.getFirst());
        Main.chat(content, fileId.getFirst());
        try {
            // 延迟 5 秒
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            // 处理中断异常
            e.printStackTrace();
        }
        return SuccessModelResult.success(ChatDocUtil.getSdf().toString());
    }

    @Autowired
    private CustomWebSocketServer webSocketServer;

    @GetMapping("/websocket-data")
    public List<String> getWebSocketData() {
        // 假设我们在 WebSocket 服务器中保存了收到的消息
        List<String> messages = new ArrayList<>();
        for (WebSocket conn : webSocketServer.getConnections()) {
            // 这里只是一个示例，实际需要根据实现情况将消息整理到列表中
            messages.add("Message from " + conn.getRemoteSocketAddress());
        }
        return messages;
    }
}
